//
//  SimSegmentScrollView.m
//  Pods
//
//  Created by LiuXubin on 16/5/4.
//
//

#import "SimScrollSegmentView.h"
#import "UIView+SimAdditions.h"
#import "SimDefine.h"

#define kSimScrollSegmentViewBaseTag 10

@interface SimScrollSegmentView ()
{
    UIView *_belowStrokeView;
    
}

@end

@implementation SimScrollSegmentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        _currentIndex = NSNotFound;
        _initIndex = 0;
        _itemWidth = 100;
    }
    
    return self;
}

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    [super willMoveToWindow:newWindow];
    if (newWindow) {
        if (_currentIndex == NSNotFound) {
            [self reloadData];
        }
    }
}


- (void)reloadData
{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    
    for (int i = 0; i < _itemCount; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*_itemWidth, 0, _itemWidth, self.height)];
        btn.tag = kSimScrollSegmentViewBaseTag + i;
        [btn addTarget:self action:@selector(clickOnItem:) forControlEvents:UIControlEventTouchUpInside];
        if (_setupItemBlock) {
            _setupItemBlock(btn, i);
        }
        [self addSubview:btn];
                
        if (i == _itemCount-1) {
            self.contentSize = CGSizeMake(btn.right, self.height);
        }
    }
    
    if (_belowStrokeColor) {
        _belowStrokeView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-2, _itemWidth, 2)];
        _belowStrokeView.backgroundColor = _belowStrokeColor;
        if (_setupBelowStrokeBlock) {
            _setupBelowStrokeBlock(_belowStrokeView);
        }
        [self addSubview:_belowStrokeView];
    }
    
    NSInteger index = 0;
    if (_initIndex < _itemCount) {
        index = _initIndex;
    }
    
    [self updateCurrentIndex:index animated:NO notify:NO];
}

- (UIButton *)btnAtIndex:(NSInteger)index
{
    return (UIButton *)[self viewWithTag:kSimScrollSegmentViewBaseTag+index];
}

- (void)updateCurrentIndex:(NSInteger)index animated:(BOOL)animated
{
    [self updateCurrentIndex:index animated:animated notify:YES];
}

- (void)updateCurrentIndex:(NSInteger)index animated:(BOOL)animated notify:(BOOL)notify
{
    if (_currentIndex != index) {
        UIButton *preBtn = [self btnAtIndex:_currentIndex];
        if (preBtn) {
            if (_itemSelectedChangedBlock) {
                _itemSelectedChangedBlock(preBtn, _currentIndex, NO);
            }
            else{
                preBtn.selected = NO;
            }
        }
        
        _currentIndex = index;
        if (notify) {
            if (_didSelectIndexBlock) {
                _didSelectIndexBlock(index);
            }
        }
        
        
        UIButton *currentBtn = [self btnAtIndex:_currentIndex];
        if (currentBtn) {
            if (_itemSelectedChangedBlock) {
                _itemSelectedChangedBlock(currentBtn, _currentIndex, YES);
            }
            else{
                currentBtn.selected = YES;
            }
            
            CGFloat offX = Range(currentBtn.centerX-self.width/2, 0, MAX(self.contentSize.width-self.width, 0));
            [UIView animateWithDuration:animated ? 0.3 : 0
                                  delay:0
                                options:UIViewAnimationOptionCurveLinear
                             animations:^{
                                 self.contentOffset = CGPointMake(offX, 0);
                                 _belowStrokeView.left = _currentIndex * _itemWidth;
                             } completion:^(BOOL finished) {
                                 
                             }];

        }
    }
}

- (void)clickOnItem:(UIButton *)btn
{
    [self updateCurrentIndex:btn.tag-kSimScrollSegmentViewBaseTag animated:YES notify:YES];
}

@end
